package com.epam.kulibaba.helpers.testSupport;

import java.util.HashMap;
import java.util.Map.Entry;

import org.openqa.selenium.WebDriver;
import org.testng.Assert;
import org.testng.Reporter;

import com.epam.kulibaba.components.catalog.ProductsCatalog;
import com.epam.kulibaba.components.catalog.linkSpecialOffers.LinkToSpecialOffers;
import com.epam.kulibaba.components.compare.panelLevelUp.PanelLevelUp;
import com.epam.kulibaba.components.compareTable.CompareTable;
import com.epam.kulibaba.components.header.HeaderPage;
import com.epam.kulibaba.components.productDescription.ProductDescription;
import com.epam.kulibaba.components.specialPrice.SpecialPrice;

public class ProductDescriptionHelper {

	public static HashMap<String, String> getProductOptions(
			ProductsCatalog productCatalog, int productNumber,
			ProductDescription itemDescription, PanelLevelUp panelLevelUp) {
		Reporter.log("get product " + productNumber + " options");
		HashMap<String, String> itemOptions = null;
		ProductsCatalog.goToProductPage(productCatalog, productNumber);
		itemOptions = ProductDescription.getDescription(itemDescription);
		PanelLevelUp.goToCategoryLevelUp(panelLevelUp);
		return itemOptions;
	}

	
	public static void checkDescriptionEqualsCompareDescription(
			HashMap<String, String> productDescriptions,
			HashMap<String, String> productDescriptionsCompare) {
		Reporter.log("check product description on product page Equals product description on compare page");
		for (Entry<String, String> entry : productDescriptions.entrySet()) {
			String key = entry.getKey();
			Assert.assertEquals(
					entry.getValue()
							.equals(productDescriptionsCompare.get(key)), true);
		}
	}


	public static void CheckAllProductsDescriptionsInCompare(
			Double numbersOfProducts, ProductsCatalog productCatalog,
			ProductDescription itemDescription,
			PanelLevelUp categoryMainPageLink, CompareTable compareTable,
			PanelLevelUp panelLevelUp) {
		for (int itemNumber = 1; itemNumber <= numbersOfProducts; itemNumber++) {
			Reporter.log("Get and check description  for product number "
					+ itemNumber + " in product catalog");
			HashMap<String, String> itemOptions = ProductDescriptionHelper
					.getProductOptions(productCatalog, itemNumber,
							itemDescription, categoryMainPageLink);
			ProductsCatalog.addProductToCompare(productCatalog, itemNumber);
			ProductsCatalog.goToProductsCompare(productCatalog, itemNumber);

			ProductDescriptionHelper.checkDescriptionEqualsCompareDescription(
					itemOptions, CompareTable
							.productDescriptionsInCompare(compareTable,
									itemNumber));
			PanelLevelUp.goToCategoryLevelUp(panelLevelUp);
		}

	}

	public static void CheckProductsInformation(WebDriver driver, ProductsCatalog productCatalog,
			ProductDescription itemDescription,
			PanelLevelUp categoryMainPageLink, Double numbersOfProducts, LinkToSpecialOffers linkBarPrice, HeaderPage header, HeaderPage searchButton, SpecialPrice resultTable) {
		for (int itemNumber = 1; itemNumber <= numbersOfProducts; itemNumber++) {			
			System.out.println("  dfgskl  " + itemNumber);
			 String productHref = ProductsCatalog.getLinkOnProductPage(productCatalog, itemNumber);
			 String productNameToSearch = ProductsCatalog.getNameToSearchProduct(productCatalog, itemNumber);
			
			 HashMap<String, String> itemOptions = ProductDescriptionHelper
					.getProductOptions(productCatalog, itemNumber,
							itemDescription, categoryMainPageLink);
			LinkToSpecialOffers.goToSpecialOffers(linkBarPrice);
			SearchPanelHelpers.searchProductByName(header, searchButton, productNameToSearch);
			SpecialOffersListHelper.checkDescriptionLinkInResultTable(productHref, resultTable);
			driver.navigate().back();
			driver.navigate().back();
		}
	}

	
}
